package com.example.weather.data

import android.util.SparseArray
import android.view.Menu
import android.view.MenuItem
import androidx.activity.viewModels
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.library_base.base.BaseActivity
import com.example.library_base.router.WeatherRouterPath
import com.example.library_base.util.LogUtil
import com.example.weather.BR
import com.example.weather.R
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

@AndroidEntryPoint
@Route(path = WeatherRouterPath.WEATHER_INSERT_DATA)
class InsertDataActivity: BaseActivity(R.layout.activity_insert_data) {

    private val mViewModel by viewModels<LocalDataViewModel>()

    @Inject
    lateinit var userModel: UserModel

    @JvmField
    @Autowired(name = "name")
    var name: String? = null

    override fun getVariableSparseArray(): SparseArray<Any> {
        val variables = SparseArray<Any>()
        variables.put(BR.localDataViewModel, mViewModel)
        variables.put(BR.userModel, userModel)
        return variables
    }

    override fun initView() {
        LogUtil.debug("InsertDataActivity", "接收传参： $name")
    }

    override fun requestData() {
        LogUtil.debug("InsertDataActivity", "接收到的页面传参$name")
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_insert, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when(item.itemId){
            R.id.action_insert->{
                //插入数据到数据库中
                mViewModel.insertData(userModel)
            }
        }
        return super.onOptionsItemSelected(item)
    }
}